<?php

require_once "../com/dtos/Cliente.php";
include("../DB.php");
//conection info
class Clientes {

    public function getLista() {
		$query = "SELECT * from CLN_CLIENTES WHERE CLNACTIVO = true";
        $result = mysql_query($query);
        $cliente = null;
        $out = array();
        while ($row = mysql_fetch_assoc($result)) {
	        $cliente = new cliente();
	    	$cliente->pk=$row['CLNPK'];
	    	$cliente->nombre = $row['CLNNOMBRE'];
			$out[] = $cliente;
        }
    	return $out;
    }

    public function nuevo($llegada){
		$cliente = (object) $llegada;
    	$query = "INSERT INTO CLN_CLIENTES (CLNNOMBRE) " .
    			 "VALUES ('$cliente->nombre') ";
    	mysql_query($query);
    	if (mysql_affected_rows() == 1){
    		return ;
    	}else{
    		throw new exception("Error al agregar\n".mysql_error()."\n".$query);
    		//throw new exception($query);
    	}
    }

    public function borrar($pk){
    	$query = "UPDATE CLN_CLIENTES SET " .
    			"CLNACTIVO = false " .
    			"WHERE CLNPK = $pk";
    	mysql_query($query);
    	if (mysql_affected_rows() == 1){
    		return ;
    	}else{
    		throw new exception("Error al borrar\n".mysql_error());
    	}
    }

    public function consultar($pk) {
		$query = "SELECT * from CLN_CLIENTES where CLNPK = '$pk' and CLNACTIVO = true";
        $result = mysql_query($query);
        $cliente = null;
        if (mysql_num_rows($result) > 0){
        	$row = mysql_fetch_array($result);
	        $cliente = new cliente();
	    	$cliente->pk=$row['CLNPK'];
	    	$cliente->nombre = $row['CLNNOMBRE'];
        }else{
        	throw new Exception("cliente no válido");
        }
    	return $cliente;
    }

    public function edit($llegada){
		$cliente = (object) $llegada;
    	$query = "UPDATE CLN_CLIENTES SET " .
    			"CLNNOMBRE = '$cliente->nombre' " .
    			"WHERE CLNPK = $cliente->pk";
    	mysql_query($query);
    	if (mysql_affected_rows() == 1){
    		return ;
    	}else{
    		throw new exception("Error al editar\n".mysql_error());
    		//throw new exception($query);
    	}
    }
}
?>